package com.dazhewan.project.component.menubar;

import com.dazhewan.project.component.SettingDialog;
import com.dazhewan.project.properties.GlobalSettting;
import com.dazhewan.utils.FunctionUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author: zheng
 * @code date: 2022/10/10
 * @code className: MyMenuBar
 * @code description:
 */
public class MyMenuBar {
    private final JMenuBar menuBar = new JMenuBar();
    private JFrame jFrame;

    //组装菜单
    public MyMenuBar(JFrame jFrame) {
        FunctionUtil.addContainer(menuBar);
        //添加文字识别相关的菜单
        addDiscernMenu();
        //添加设置菜单
        addSettingMenu(jFrame);
    }

    /**
     * @param jFrame
     * @method: 添加设置相关的菜单栏
     */
    private void addSettingMenu(JFrame jFrame) {
        final JMenu settingMenu = new JMenu("设置");
        final SettingDialog settingDialog = new SettingDialog(jFrame);
        addMouseListenerForJMenuItem(settingMenu,new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                final JDialog dialog = settingDialog.getSettingDialog();
                dialog.setVisible(true);
            }
        });

        menuBar.add(settingMenu);
    }

    /**
     * @method: 添加文字识别相关的菜单项
     */
    private void addDiscernMenu() {
        final JMenu discernMenu = new JMenu("文字识别");
        //截图类
        final JMenuItem screenItem = new JMenuItem("全屏截图");
        final JMenuItem chipItem = new JMenuItem("划取截图");
        final JMenuItem activeItem = new JMenuItem("活动窗口截取");
        discernMenu.add(screenItem);
        discernMenu.add(chipItem);
        discernMenu.add(activeItem);
        discernMenu.addSeparator();

        chipItem.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent event) {
                try {
                    Robot robot = new Robot();
                    robot.keyPress(GlobalSettting.DRAW_SCREEN_CUT_KEY_ALL);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
        });

        final JMenuItem uploadItem = new JMenuItem("上传图片");
        discernMenu.add(chipItem);

        menuBar.add(discernMenu);
    }

    /**
     * @method: 添加菜单组件并添加事件监听器
     */
    private void addMouseListenerForJMenuItem(JMenuItem menuItem, MouseListener mouseListener) {
        menuItem.addMouseListener(mouseListener);
    }

    public JMenuBar getMenuBar() {
        return menuBar;
    }
}
